package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.map.StudentMapper;

import com.example.demo.service.Impl.*;
import com.example.demo.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import javax.validation.Valid;
import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class IController {
    private int studentNo;

    @RequestMapping("/welcome")
    public String hello(Model model) {
        String name = "woodpecker";
        model.addAttribute("name", name);
        return "test";
    }


    @RequestMapping(value = "/SelectCourse", method = RequestMethod.GET)
    public String nextPage(@AuthenticationPrincipal Account account,@ModelAttribute("temp") @Valid Temp temp, BindingResult bindingResult,
                           @RequestParam("stuNo") Integer stuNo, @RequestParam("stuName") String stuName
            , @RequestParam("stuGrade") Integer stuGrade, @RequestParam("college") String college
            , @RequestParam("profession") String professions, Model model) {
        if (bindingResult.hasErrors()) {
            System.out.println("格式错误");
            System.out.println(bindingResult.getFieldError().getField());
            return "PersonInfo";
        }
        System.out.println(account.getUsername());
        System.out.println("college=" + college);
        System.out.println("profession=" + professions);
        studentNo = stuNo.intValue();
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i1 = mapper.StudentExist(studentNo);
        if (i1 > 0) {
            System.out.println("该用户已存在");
        } else {
            int grade = stuGrade.intValue();
            Student student = new Student();
            Faculty faculty = new Faculty();
            //查询学院编号
            FacultyServiceImpl service = new FacultyServiceImpl();
            int i2 = service.queryByName(college);
            faculty.setFacNo(i2);
            Profession profession = new Profession();
            //查询专业编号
            ProfessionServiceImpl professionService = new ProfessionServiceImpl();
            int i3 = professionService.queryByProName(professions);
            profession.setProNo(i3);
            faculty.setFacName(college);
            profession.setFaculty(faculty);
            profession.setProName(professions);
            student.setProfession(profession);
            Sex sex = new Sex();
            sex.setSexName("女");
            student.setSex(sex);
            student.setStuGrade(grade);
            student.setStuName(stuName);
            student.setStuNo(studentNo);
            int i = mapper.addStudent(student);
            if (i > 0) {
                System.out.println("插入成功");
            }
        }
        sqlSession.close();
        //查询课程数据，返回给页面
        CourseServiceImpl courseService = new CourseServiceImpl();
        List<Course> courses = courseService.queryAllCourse();
        model.addAttribute("courseList", courses);
        model.addAttribute("userName",account.getUsername());
        return "SelectCourse";
    }

    @RequestMapping("/displaySelect")
    public String display(@AuthenticationPrincipal Account account,@RequestParam List<String> selCou, Model model) {
        //获取课程信息
        //根据课程编号查询课程信息
        List<Course> coursesList = new ArrayList<>();
        //利用stream将字符串列表转化为整型列表:java8新特性
        List<Integer> integerList = selCou.stream().map(Integer::parseInt).collect(Collectors.toList());
        SelectCourseService selectCourseService = new SelectCourseService();
        selectCourseService.insAllSelCou(studentNo, integerList);
        //根据课程编号查询课程信息
        CourseServiceImpl service = new CourseServiceImpl();
        for (Integer integer : integerList) {
            Course course = service.queryCourseById(integer);
            coursesList.add(course);
        }
        //根据学号查询学院等的相关信息
        StudentServiceImpl studentService = new StudentServiceImpl();

        Student student1 = studentService.queryById(studentNo);

        model.addAttribute("courseList", coursesList);
        model.addAttribute("student", student1);
        model.addAttribute("userName",account.getUsername());
        return "displayInfo";
    }
}
